package com.dexgdx.game.json;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Json;
import com.dexgdx.game.constants.EMoveDirection;
import com.dexgdx.game.constants.EPlayerType;
import com.dexgdx.game.util.ObjectIdentification;

public class JSONManager {
	
	@SuppressWarnings("unchecked")
	public static ArrayList<JSONActor> getJSONActor(){
		Json json = new Json();
        return json.fromJson(ArrayList.class, Gdx.files.local("data/actors.json"));
    }
    
	@SuppressWarnings("unchecked")
	public static ArrayList<JSONAnimationObject> getJSONAnimationObject(){
		Json json = new Json();
        return json.fromJson(ArrayList.class, Gdx.files.local("data/animations.json"));
    }
        
    public static void saveJSONActor(){
		Json json = new Json();
		ArrayList<JSONActor> actors = new ArrayList<JSONActor>();
		actors.add(new JSONActor(EPlayerType.PLAYER, true, 1000, 200, -1, -1, "", -1, "SOUTH", new ObjectIdentification(2, "Jenna")));
		actors.add(new JSONActor(EPlayerType.ENEMY, true, 320, 250, -1, -1, "", -1, "SOUTH", new ObjectIdentification(1, "Tim")));
//		actors.add(new JSONActor(EPlayerType.PLAYER, true, 1000, 3000, 10, 10, "", 5, "SOUTH", new PlayerIdentification(2, "Jenna")));
		actors.add(new JSONActor(EPlayerType.PLAYER, true, 160, 0, -1, -1, "", -1, "SOUTH", new ObjectIdentification(2, "Jenna")));
		actors.add(new JSONActor(EPlayerType.ENEMY, true, 1000, 420, -1, -1, "", -1, "SOUTH", new ObjectIdentification(1, "Tim")));
		actors.add(new JSONActor(EPlayerType.ENEMY, true, 1000, 300, -1, -1, "", -1, "SOUTH", new ObjectIdentification(1, "Tim")));

		String jsonString = json.prettyPrint(actors);
//    	System.out.print(jsonString);
		
		FileHandle file = Gdx.files.local("data/actors.json");
		file.writeString(jsonString, false);
    }
    
    public static void saveJSONAnimationObject(){
		Json json = new Json();
		ArrayList<JSONAnimationObject> jSONAnimationObjects = new ArrayList<JSONAnimationObject>();

		jSONAnimationObjects.add(loadTestObject(
				EMoveDirection.SOUTH_WEST, EMoveDirection.SOUTH, EMoveDirection.SOUTH_EAST, EMoveDirection.WEST, 
				EMoveDirection.NORTH_WEST, EMoveDirection.EAST, EMoveDirection.NORTH_EAST, EMoveDirection.NORTH,
				"naked_man_walk.png", 9, 8, 0, 1, 1, 1));

		jSONAnimationObjects.add(loadTestObject(
				EMoveDirection.SOUTH, EMoveDirection.SOUTH_WEST, EMoveDirection.WEST, EMoveDirection.NORTH_WEST,
				EMoveDirection.NORTH, EMoveDirection.NORTH_EAST, EMoveDirection.EAST, EMoveDirection.SOUTH_EAST,
				"warrior_woman_walk.gif", 8, 8, 7, 2, 1, 1));

		jSONAnimationObjects.add(loadTestObject(
				EMoveDirection.SOUTH, EMoveDirection.SOUTH_WEST, EMoveDirection.WEST, EMoveDirection.NORTH_WEST,
				EMoveDirection.NORTH, EMoveDirection.NORTH_EAST, EMoveDirection.EAST, EMoveDirection.SOUTH_EAST,
				"normal_woman_walk.png", 9, 8, 0, 3, 1, 1));
		
		String jsonString = json.prettyPrint(jSONAnimationObjects);
//    	System.out.print(jsonString);
		
		FileHandle file = Gdx.files.local("data/animations.json");
		file.writeString(jsonString, false);
    }
    
    private static JSONAnimationObject loadTestObject(
    		EMoveDirection moveDirection1,
    		EMoveDirection moveDirection2,
    		EMoveDirection moveDirection3,
    		EMoveDirection moveDirection4,
    		EMoveDirection moveDirection5,
    		EMoveDirection moveDirection6,
    		EMoveDirection moveDirection7,
    		EMoveDirection moveDirection8,
    		String spriteSheetPath, int columnSize,
			int rowSize, int stopRowIndex, int playerNameId, int costumeNameId,
			int animationType){
    	
		List<EMoveDirection> eMoveDirections = new ArrayList<EMoveDirection>();
		eMoveDirections.add(moveDirection1);
		eMoveDirections.add(moveDirection2);
		eMoveDirections.add(moveDirection3);
		eMoveDirections.add(moveDirection4);
		eMoveDirections.add(moveDirection5);
		eMoveDirections.add(moveDirection6);
		eMoveDirections.add(moveDirection7);
		eMoveDirections.add(moveDirection8);
		return new JSONAnimationObject(spriteSheetPath, columnSize, rowSize, stopRowIndex, playerNameId, costumeNameId, animationType, eMoveDirections);
    }
    
    public static void main(String[] args) {
//    	saveJSONAnimationObject();
    	saveJSONActor();
	}
}
